package my.yuan.fkzapp.validator;

import my.yuan.fkzapp.vo.DemoVO;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @author qy
 * @description DemoValidator
 * @date 2021-10-03
 */
@Component
public class DemoValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return DemoVO.class.equals(clazz);
    }

    @Override
    public void validate(Object obj, Errors e) {
        DemoVO demoVO = (DemoVO) obj;
        ValidationUtils.rejectIfEmpty(e, "value", "value不能为空");
        int length = demoVO.getName().length();
        if (length < 1 || length > 4) {
            e.rejectValue("name", "name length must betweent 1 and 4");
        }
    }
}
